#include "config.h"

Config::Config() {
    initAllArrays();
}

void Config::initAllArrays() {
    for (int i = 0; i < NUMBEROFBOTTLES; i++){
        activeBottles[i] = false;
        volumeBottles[i] = -1;
        volumeSludge[i] = -1;
        volumeSubstrate[i] = -1;
        lowerCalValue[i] = -1;
        upperCalValue[i] = -1;
        lowerCalPressure[i] = -1*-65536;
        upperCalPressure[i] = -1;
        calibrationMultiplicator[i] = 0.47762;
        calibrationOffset[i] = 1.2957;
        if (i < 4)
            bottleGroupNames[i] = "";
    }
    createdTime = QDateTime::currentDateTimeUtc();
    projectName = "";
}


Config::Config( const Config& other ) {
        *this = other;
}

Config& Config::operator=( const Config& other ) {
    if( this == &other )
        return *this;

    for (int i = 0; i < NUMBEROFBOTTLES; i++){
        activeBottles[i]    = other.activeBottles[i];
        volumeBottles[i] = other.volumeBottles[i];
        volumeSludge[i] =other.volumeSludge[i];
        volumeSubstrate[i] = other.volumeSubstrate[i];

        if (i < 4)
            bottleGroupNames[i]  = other.bottleGroupNames[i];
    }
    createdTime   = other.createdTime;
    projectName   = other.projectName;

    return *this;
}


void Config::setProjectName(QString prName) {
    QDateTime dateTime = QDateTime::currentDateTimeUtc();
    projectName = dateTime.toString("yyyy-MM-dd-hh-mm") + "-" + prName + ".bimos";
}

void Config::setActiveBottles(bool* actBottles){
    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        activeBottles[i] = actBottles[i];
    }
}

void Config::setVolumeBottle(float* bVolume){
    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        volumeBottles[i] = bVolume[i];
    }
}

void Config::setVolumeSludge(float* bVolume){
    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        volumeSludge[i] = bVolume[i];
    }
}

void Config::setVolumeSubstrate(float* bVolume){
    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        volumeSubstrate[i] = bVolume[i];
    }
}

void Config::setBottleGroupNames(QString *names){
    for (int i = 0; i < 4; i++) {
        bottleGroupNames[i] = names[i];
    }
}

void Config::setLowerCalibrationValue(int connector, float voltValue,float pressure) {
    lowerCalValue[connector] = voltValue;
    lowerCalPressure[connector] = pressure;
}

void Config::setUpperCalibrationValue(int connector, float voltValue, float pressure) {
    upperCalValue[connector] = voltValue;
    upperCalPressure[connector] = pressure;
}

void Config::calculateCalibration() {

    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        if (lowerCalValue[i] != -1 && upperCalValue[i] != -1) {
            calibrationMultiplicator[i] = (upperCalPressure[i] - lowerCalPressure[i])/(upperCalValue[i] - lowerCalValue[i]);
            calibrationOffset[i] = lowerCalPressure[i] - calibrationMultiplicator[i]*lowerCalValue[i];
            calibrationMultiplicator[i] /= 1000;
            calibrationOffset[i] /= 1000;
        }
    }
    appendCalibrationToConfigFile();

}

/** Format of config file:
  *
  * projectname \t $projectname \n
  * activeBottles \t 1 \t 2 \t ... \t \n
  * volumeBottles \t 290 \t 290 \t ... \t \n
  * volumeSludge \t 10 \t 10 \t ... \t \n
  * volumeSubstrate \t 5 \t 5 \t ... \t \n
  * BottleGroup1Name \t $ groupBottleName1 \n
  * ...
  * BottleGroup4Name \t $groupBottleName4 \n
  * Standardcalibration \t $0.47762 * Vout  + 0.12957
  * CalMult \t 10 \t 10 ... \t \n
  * TODO: Link to output file
 */
void Config::writeConfigToFile() {
    file = new QFile("./" + projectName);
    out = new QTextStream(file);
    file->open(QIODevice::WriteOnly | QIODevice::Text);

    // Set project name
    *out << "projectname" << "\t" << projectName << "\n";
    out->flush();

    // Set active bottles
    *out << "activeBottles" << "\t";
    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        if (activeBottles[i]) {
            *out << i << "\t";
        }
    }
    *out << "\n";
    out->flush();

    *out << "volumeBottles" << "\t";
    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        *out << volumeBottles[i] << "\t";
    }
    *out << "\n";
    out->flush();

    *out << "volumeSludge" << "\t";
    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        *out << volumeSludge[i] << "\t";
    }
    *out << "\n";
    out->flush();

    *out << "volumeSubstrate" << "\t";
    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        *out << volumeSubstrate[i] << "\t";
    }
    *out << "\n";
    out->flush();
    for (int i = 0; i < 4; i++) {
        *out << "BottleGroup"+QString::number(i)+"Name" << "\t" << bottleGroupNames[i] << "\n";
    }
    *out << "\n";
    out->flush();

    *out << "Standardcalibration" << "\t" << "0.47762 * x + 0.12957";
    *out << "\n";
    out->flush();
}

void Config::appendCalibrationToConfigFile() {
    *out << "CalMult" << "\t";
    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        *out << calibrationMultiplicator[i]  << "\t";
    }
    *out << "\n";
    out->flush();

    *out << "CalOffset" << "\t";
    for (int i = 0; i < NUMBEROFBOTTLES; i++) {
        *out << calibrationOffset[i]  << "\t";
    }
    *out << "\n";
    out->flush();


}

QString Config::getProjectName() {
    return projectName;
}

bool* Config::getActiveBottles() {
    return activeBottles;
}

float * Config::getVolumeBottle() {
    return volumeBottles;
}
float * Config::getVolumeSludge() {
    return volumeSludge;
}
float * Config::getVolumeSubstrate() {
    return volumeSubstrate;
}

QString * Config::getBottleGroupNames() {
    return bottleGroupNames;
}

float *Config::getMultiplicator() {
    return calibrationMultiplicator;
}
float *Config::getOffset() {
    return calibrationOffset;

}




